//
//  MyGolfNormalController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/6/27.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class MyGolfNormalController: CBBaseViewController {
    
    @IBOutlet weak var lblDate: UILabel!
    
    @IBOutlet weak var lblPlace: UILabel!
    
    @IBOutlet weak var btnAM: UIButton!
    
    @IBOutlet weak var btnPM: UIButton!
    
    @IBOutlet weak var btnReserve: UIButton!
    
    var model:MyGolfNormalReserveInfoModel = MyGolfNormalReserveInfoModel()
    
    //选择从场次，上午0 下午1
//    var time:String = "0"
    var time:Variable<String?> = Variable(nil)
    
    var upEnable = true
    var downEnable = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "我的高尔夫"
        
        initData()
        
        btnAM.rx.tap.asObservable()
            .subscribe(onNext:{ [unowned self] _ in
                if self.upEnable {
                    self.time.value = "0"
                }
            }).disposed(by: disposeBag)
        
        btnPM.rx.tap.asObservable()
            .subscribe(onNext:{ [unowned self] _ in
                if self.downEnable {
                    self.time.value = "1"
                }
            }).disposed(by: disposeBag)
        
        let input = time.asObservable().share(replay:1)
        
        time.asObservable()
            .subscribe(onNext:{ [unowned self] tm in
                if tm == "0" {
                    self.setButtonSelect(btn: self.btnAM, isSelect: true)
                    self.setButtonSelect(btn: self.btnPM, isSelect: false)
                } else {
                    self.setButtonSelect(btn: self.btnAM, isSelect: false)
                    self.setButtonSelect(btn: self.btnPM, isSelect: true)
                }
            }).disposed(by: disposeBag)
        
        input.map { $0 != nil }
            .subscribe(onNext:{ [unowned self] isValid in
                self.btnReserve.isEnabled = isValid
                self.btnReserve.alpha = isValid ? 1.0 : 0.5
            }).disposed(by: disposeBag)
    
        //立即预约
        btnReserve.rx.tap.asObservable()
            .withLatestFrom(input)
            .subscribe(onNext:{ [unowned self] tm in
                self.reserveAction(tm: tm ?? "")
            }).disposed(by: disposeBag)
        
    }
    
    func initData() {
        SVProgressHUD.show()
        NetworkTool.requestData(url: ActionURL.GET_GOLF_NORMAL_RESERVE_INFO, param: ["uid":MainTools.shared.userModel?.uid ?? ""])
            .map { (json) -> MyGolfNormalReserveInfoModel? in
                return MyGolfNormalReserveInfoModel.mj_object(withKeyValues: json["rows"].dictionaryValue["data"]?.dictionaryObject)
            }.catchErrorJustReturn(nil)
            .subscribe(onNext:{ [unowned self] opModel in
                if let tmpModel = opModel {
                    self.model = tmpModel
                    self.flushView()
                }
                SVProgressHUD.dismiss()
            }).disposed(by: disposeBag)
    }
    
    func flushView() {
        lblDate.text = model.start_time
        lblPlace.text = model.name
        btnAM.setTitle("上午 (\(model.m_num))", for: .normal)
        btnPM.setTitle("下午 (\(model.a_num))", for: .normal)
        upEnable = model.m_num > 0
        downEnable = model.a_num > 0
        setButtonEnable(btn: btnAM, isEnable: upEnable)
        setButtonEnable(btn: btnPM, isEnable: downEnable)
    }
    
    override func setupRightBtnAction() {
        self.view.endEditing(true)
        let btnDetail = UIButton(text: "我的预约", color: .white, fontSize: 15.wr)
        btnDetail.cb_size = CGSize(width: 30, height: 30)
        btnDetail.rx.tap
            .subscribe(onNext:{ [weak self] _ in
                let reserveVc = MyGolfReserveController()
                self?.navigationController?.pushViewController(reserveVc, animated: true)
            }).disposed(by: disposeBag)
        let rightBarButtonItem:UIBarButtonItem =
            UIBarButtonItem(customView: btnDetail)
        rightBarButtonItem.tintColor = .white
        self.navigationItem.rightBarButtonItem = rightBarButtonItem
    }
    
    //立即预约
    func reserveAction(tm:String) {
        NetworkTool.requestData(url: ActionURL.GOLF_RESERVE_EXP, param: [
                "uid":MainTools.shared.userModel?.uid ?? "",
                "golf_id":model.id,
                "time":tm,
                ]).map { (json) -> Bool in
                    return true
                }.catchErrorJustReturn(false)
            .subscribe(onNext:{ [unowned self] isSuccess in
                if isSuccess {
                    MainTools.makeToast(message: "预约成功")
                    self.initData()
                }
            }).disposed(by: disposeBag)
    }
    
    func setButtonSelect(btn:UIButton, isSelect:Bool) {
        btn.setTitleColor(UIColor(hex: isSelect ? 0xffffff : 0x333333), for: .normal)
        btn.backgroundColor = UIColor(hex: isSelect ? 0xff5722 : 0xffffff)
        btn.borderWidth = isSelect ? 0.0 : 1.0
    }
    
    func setButtonEnable(btn:UIButton, isEnable:Bool) {
        setButtonSelect(btn: btn, isSelect: false)
        btn.setTitleColor(UIColor(hex: isEnable ? 0x333333 : 0xcccccc), for: .normal)
        btn.borderColor = UIColor(hex: isEnable ? 0xbfbfbf : 0xe5e5e5)
    }
    
}
